<?php
include_once('../../php/postgre.php');

$act = '';
if (isset($_POST['act']))
    $act = $_POST['act'];

if ($act == 'tmpl_program') {
    $idskpd = $_POST['idSk'];
    if (is_numeric($idskpd)) {
        echo 'ok___';
        view_data($idskpd);
    } else {
        echo "er";
    }
} else if ($act == 'rincProg') {
    $bagian = htmlentities($_POST['bgian']);
    $id = abs($_POST[id]);
//    echo $bagian." => ".$id;
    view_tbelRinc($id, $bagian);
} else if ($act == 's_rincian') {
    $kode = htmlentities($_POST['kde']);
    $idUs = $_POST['id_us'];
    $status = $_POST['status'];
    $apbdkab = $_POST['apbdkab'];
    $apbdprov = $_POST['apbdprov'];
    $apbn = $_POST['apbn'];
    $ket = $_POST['keterangan'];
    $prioritas = $_POST['prioritas'];
    if (count($idUs) > 0) {
        if ($kode == 'sk') {
            foreach ($idUs as $key => $value) {
                $stt = ($status[$key] == '0') ? 'NULL' : $status[$key];
                $apKab = ($apbdkab[$key] == '0' || empty($apbdkab[$key])) ? '0' : $apbdkab[$key];
                $apProv = ($apbdprov[$key] == '0' || empty($apbdprov[$key])) ? '0' : $apbdprov[$key];
                $apN = ($apbn[$key] == '0' || empty($apbn[$key])) ? '0' : $apbn[$key];
                $ketRangan =  str_replace("'", "''", $ket[$key]);
                $prio = ($prioritas[$key] == '0') ? 'NULL' : $prioritas[$key];
                $ff = exec_query("UPDATE mus_rinc_usulan_skpd SET status =" . $stt . ", apbdkab = $apKab, apbdprov = $apProv, apbn = $apN, prioritas = $prio,ket = '$ketRangan' WHERE id_rinc_usulan_skpd = " . $value);
                if (!$ff) {
                    $rslt = '2';
                    $er = error_get_last();
                    foreach ($er as $key => $value) {
                        $error.= ($key == 0) ? 'Error in : ' . $value : $value;
                    }
                    break 1;
                } else {
                    $rslt = '1';
                }
				
				logs('Memverifikasi usulan SKPD','ID usulan '.$value);
            }
			
            echo $rslt . "___" . $error;
        } else if ($kode == 'kec') {
            foreach ($idUs as $key => $value) {
                $stt = ($status[$key] == '0') ? 'NULL' : $status[$key];
                $apKab = ($apbdkab[$key] == '0' || empty($apbdkab[$key])) ? '0' : $apbdkab[$key];
                $apProv = ($apbdprov[$key] == '0' || empty($apbdprov[$key])) ? '0' : $apbdprov[$key];
                $apN = ($apbn[$key] == '0' || empty($apbn[$key])) ? '0' : $apbn[$key];
                $ketRangan = str_replace("'", "''", $ket[$key]);
                $prio = ($prioritas[$key] == '0') ? 'NULL' : $prioritas[$key];              
                $ff = exec_query("UPDATE mus_rinc_usulan_kec SET status =" . $stt . ",apbdkab = $apKab,apbdprov = $apProv, apbn = $apN, prioritas = $prio, ket = '$ketRangan' WHERE id_rinc_usulan_kec = " . $value);
                if (!$ff) {
                    $rslt = '2';
                    $er = error_get_last();
                    $error = 'Error in : ';
                    foreach ($er as $value) {
                        $error.= ($key == 0) ? 'Error in : ' . $value : $value;
                    }
                    break 1;
                } else {
                    $rslt = '1';
                }
				
				logs('Memverifikasi usulan Kecamatan','ID usulan '.$value);
            }
			
            $rst = (!isset($rslt)) ? '1' : $rslt;
            echo $rslt . "___" . $error;
        }
    } else {
        echo '0___';
    }
}

function view_data($idskpd) {
    $year = date('Y');
    $data = get_datas("SELECT distinct(prog.id_program), prog.nama_program
FROM mus_usulan_skpd usk, mus_usulankecamatan uskec,mus_kegiatan keg, mus_program prog
where  keg.id_program = prog.id_program 
and ((usk.id_kegiatan = keg.id_keg and usk.id_skpd = $idskpd and usk.tahun = $year) 
OR (uskec.id_kegiatan = keg.id_keg and uskec.id_skpd = $idskpd and uskec.tahun = $year))");
    $keg = "";
    $pro1 = ""; //<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
    $no = 1;
    if (count($data) > 0) {
        foreach ($data as $dataProgram) {
            ?>        
            <tr><td><?php echo $no; ?></td><td colspan="4"><?php echo ucfirst($dataProgram['nama_program']); ?></td></tr>
            <?php
            $dtUsuSKP = get_datas("SELECT usk.id_uskpd, keg.nama_kegiatan, usk.indikator_keluaran,usk.target_hasil, usk.indikator_hasil
FROM mus_usulan_skpd usk, mus_kegiatan keg, mus_program prog where  keg.id_program = prog.id_program and (usk.id_kegiatan = keg.id_keg 
and usk.id_skpd = '$idskpd' and usk.tahun = '$year') and prog.id_program = " . $dataProgram['id_program']);
            if (count($dtUsuSKP) > 0) {
                foreach ($dtUsuSKP as $value) {
                    ?>
                    <tr><td>&nbsp;</td><td style="padding-left: 25px;"><a href='#windowRincPro' data-toggle="modal" onclick="showRincProg(this)" name="<?php echo "sk_" . $value['id_uskpd']; ?>"><?php echo ucfirst($value['nama_kegiatan']); ?></a></td>
                        <td><?php echo $value['indikator_keluaran']; ?></td>
                        <td><?php echo $value['target_hasil']; ?></td>
                        <td><?php echo $value['indikator_hasil']; ?></td>
                    </tr> 
                    <?php
                }
            }
            $dtUsuKec = get_datas("SELECT usk.id_usulankec,keg.nama_kegiatan, usk.indikator_keluaran,usk.target_hasil, usk.indikator_hasil
FROM mus_usulankecamatan usk, mus_kegiatan keg, mus_program prog where keg.id_program = prog.id_program and (usk.id_kegiatan = keg.id_keg 
and usk.id_skpd = '$idskpd' and usk.tahun = '$year') and prog.id_program = " . $dataProgram['id_program']);
            if (count($dtUsuKec) > 0) {
                foreach ($dtUsuKec as $value) {
                    ?>
                    <tr><td>&nbsp;</td><td style="padding-left: 25px;"><a href='#windowRincPro' data-toggle="modal" onclick="showRincProg(this)" name="<?php echo "kec_" . $value['id_usulankec']; ?>"><?php echo ucfirst($value['nama_kegiatan']); ?></a></td>
                        <td><?php echo $value['indikator_keluaran']; ?></td>
                        <td><?php echo $value['target_hasil']; ?></td>
                        <td><?php echo $value['indikator_hasil']; ?></td>
                    </tr> 
                    <?php
                }
            }
            $no++;
        }
    } else {
        echo "<tr><td colspan='5'><div class='alert center'>Tidak ada data</div></td></tr>";
    }
}

function view_tbelRinc($iduslnkpd, $bg) {
    $status = array('Diterima', 'Ditolak', 'Dipertimbangkan');
    $iconSt = array(1 => 'icon-terima', 2 => 'icon-tolak', 3 => 'icon-pertimbangan');

    if ($bg == 'sk') {
        ?>
        <input type='hidden' name='kde' value='sk' />
        <input type='hidden' name='act' value='s_rincian' />        
        <table class="table table-bordered table-striped geo-table">
            <tr>
                <th >No</th>
                <th >Usulan</th>
                <th >Lokasi</th>
                <th >Volume</th>
                <th >APBD Kab</th>
                <th >APBD Prov</th>
                <th >APBN</th>
                <th >Status</th>
                <th >Prioritas</th>
                <th >Keterangan</th>
            </tr>
            <?php
            $dt = get_datas("SELECT id_rinc_usulan_skpd, usulan, lokasi, valume, apbdkab, apbdprov, apbn,status,prioritas,ket FROM mus_rinc_usulan_skpd where id_uskpd = " . $iduslnkpd);
            $no = 1;
            foreach ($dt as $value) {
                $usl = (empty($value['usulan'])) ? '$nbsp;' : $value['usulan'];
                $lks = (empty($value['lokasi'])) ? '&nbsp;' : $value['lokasi'];
                $vlm = (empty($value['valume'])) ? '&nbsp;' : $value['valume'];
                ?>
                <tr><td><?php echo $no; ?></td>                
                    <td><input type='hidden' name='id_us[]' value='<?php echo $value['id_rinc_usulan_skpd']; ?>' />
                        <?php echo $usl; ?></td>
                    <td><?php echo $lks; ?></td>
                    <td><?php echo $vlm; ?></td>
                    <td><input type='text' name='apbdkab[]' style="width:100px;height:25px" value='<?php echo $value['apbdkab']; ?>' /></td>
                    <td><input type='text' name='apbdprov[]' style="width:100px;height:25px" value='<?php echo $value['apbdprov']; ?>' /></td>
                    <td><input type='text' name='apbn[]' style="width:100px;height:25px" value='<?php echo $value['apbn']; ?>' /></td>
                    <td><span id='icn_<?php echo $no; ?>' class='<?php if (!empty($value['status'])) echo $iconSt[$value['status']]; ?>'></span><select id='<?php echo $no; ?>' class='status' style="width:130px;" name="status[]"><option value=0>--Pilih status--</option><?php
                            foreach ($status as $key => $isi) {
                                $st = ($value['status'] == ($key + 1)) ? 'selected' : '';
                                echo "<option value=" . ($key + 1) . " " . $st . ">" . $isi . "</option>";
                            }
                            ?></select></td>
                    <td><select name='prioritas[]' class='span1'><option valu=0>--Pilih Prioritas--</option><?php
                            $i = 1;
                            while ($i <= count($dt)) {
                                $slc = ($i == $value['prioritas']) ? 'selected' : '';
                                echo "<option value=$i $slc>$i</option>";
                                $i++;
                            }
                            echo $value['prioritas'];
                            ?></select></td>
                    <td><textarea name='keterangan[]' ><?php echo $value['ket']; ?></textarea></td>
                </tr>
                <?php
                $no++;
            }
        } else if ($bg == 'kec') {
            ?>
            <input type='hidden' name='kde' values='kec' />
            <input type='hidden' name='act' values='s_rincian' />
            <table class="table table-bordered table-striped geo-table">
                <tr><th> No</th><th>Lokasi</th><th>Volume</th><th>APBD Kab</th><th>APBD Prov</th><th>APBN</th><th>Status</th><th>Prioritas</th><th>Keterangan</th></tr>
                <?php
                $dt = get_datas("SELECT id_rinc_usulan_kec, lokasi, volume, apbdkab, apbdprov, apbn,status,prioritas,ket FROM mus_rinc_usulan_kec where id_usulankec = " . $iduslnkpd);
                $no = 1;
                $jumData = count($dt);
                foreach ($dt as $value) {
                    $lks = (empty($value['lokasi'])) ? '&nbsp;' : $value['lokasi'];
                    $vlm = (empty($value['volume'])) ? '&nbsp;' : $value['volume'];
                    ?>
                    <tr><td><?php echo $no; ?></td>                
                        <td style='vertical-align:middle;'><?php echo $lks; ?><input type='hidden' name='id_us[]' value='<?php echo $value['id_rinc_usulan_kec']; ?>' /></td>
                        <td style='vertical-align:middle;'><?php echo $vlm; ?></td>
                        <td style='vertical-align:middle;text-align: right;'><input type='text' name='apbdkab[]' value='<?php echo $value['apbdkab']; ?>' /></td>
                        <td style='vertical-align:middle;text-align: right;'><input type='text' name='apbdprov[]' value='<?php echo $value['apbdprov']; ?>' /></td>
                        <td style='vertical-align:middle;text-align: right;'><input type='text' name='apbn[]' value='<?php echo $value['apbn']; ?>' /></td>
                        <td style='vertical-align:middle;'><span id='icn_<?php echo $no; ?>' class='<?php if (!empty($value['status'])) echo $iconSt[$value['status']]; ?>'></span><select id='<?php echo $no; ?>' class='status span2' name="status[]"><option value=0>--Pilih status--</option><?php
                                foreach ($status as $key => $isi) {
                                    $st = ($value['status'] == ($key + 1)) ? 'selected' : '';
                                    echo "<option value=" . ($key + 1) . " " . $st[$key] . ">" . $isi . "</option>";
                                }
                                ?></select></td>
                        <td style='vertical-align:middle;text-align: right;'><select name='prioritas[]' class='span1'><option valu=0>--Pilih Prioritas--</option><?php
                                $i = 1;
                                while ($i <= count($dt)) {
                                    $slc = ($i == $value['prioritas']) ? 'seleceted' : '';
                                    echo "<option value=$i $slc>$i</option>";
                                    $i++;
                                }
                                echo $value['prioritas'];
                                ?></select></td>
                    </tr>
                    <?php
                    $no++;
                }
            } else {
                ?><tr><td colspan='10'><div class='alert alert-info'>Tidak ada data</div></td></tr><?php
            }
            ?></table>
        <div class="pull-right">
            <span id='loader' style='width:30px;height:30px;'></span><span id='result'></span><button type="button" class="btn btn-primary btn-simpan-usulan"><i class="icon-white icon-check"></i>&nbsp;Simpan</button>
        </div>   
        <?php
    }
    ?>